package ru.ds.selenium.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

import java.util.Arrays;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import ru.ds.selenium.pages.InternalPage;
import ru.ds.selenium.util.MiscUtils;

public class MovieInfoPage extends InternalPage {
	public MovieInfoPage(PageManager pages) {
		super(pages);
	}
	
	@FindBy(xpath =  "//img[contains(@src,'star.png')]")
	private WebElement ownImg;
	
	@FindBy(xpath = "//img[contains(@src,'eye.png')]")
	private WebElement seenImg;
	
	@FindBy(xpath = "//img[contains(@src,'pencil.png')]")
	private WebElement editImg;
	
	@FindBy(xpath = "//img[contains(@src,'delete.png')]")
	private WebElement removeImg;
	
	public void clickOwn() {
		ownImg.click();
	}
	
	public void clickSeen() {
		seenImg.click();
	}
	
	public void clickEdit() {
		editImg.click();
	}
	
	
	public void clickDelete() {
		removeImg.click();
		wait.until(alertIsPresent()).accept();
	}
	
	public String getMovieID() {
		List<String> lstIDs = Arrays.asList(getPageURL().split("="));
		return lstIDs.get(lstIDs.size() - 1);
	}
	
	public boolean isMovieInfoPage() {
		return MiscUtils.isOnlyDigits(getMovieID());
	}
	
	public MovieInfoPage ensurePageLoaded() {
		super.ensurePageLoaded();
		wait.until(presenceOfElementLocated(By.xpath("//div[@class='maininfo_full']")));
		return this;
	}	
	
}
